package com.oa.config;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@ComponentScan(basePackages = "com.oa")
public class RootConfig {

	@Bean(name = "processEngineConfiguration")
	public ProcessEngineConfigurationImpl processEngineConfiguration(DataSource dataSource,
			EntityManagerFactory entityManagerFactory, PlatformTransactionManager transactionManager) {

		SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
		processEngineConfiguration.setDataSource(dataSource);
		processEngineConfiguration.setTransactionManager(transactionManager);
		processEngineConfiguration.setJpaEntityManagerFactory(entityManagerFactory);
		processEngineConfiguration.setJpaHandleTransaction(false);
		processEngineConfiguration.setJpaCloseEntityManager(false);
		processEngineConfiguration.setActivityFontName("宋体");
		processEngineConfiguration.setLabelFontName("宋体");
		processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
		processEngineConfiguration.setJobExecutorActivate(false);
		// 自动部署
		ClassPathResource[] deploymentResources = new ClassPathResource[] {
				new ClassPathResource("/activiti/diagram/Overtime.zip"),
				new ClassPathResource("/activiti/diagram/goout.zip"),
				new ClassPathResource("/activiti/diagram/Leave.zip"),
				new ClassPathResource("/activiti/diagram/order.zip") };
		processEngineConfiguration.setDeploymentResources(deploymentResources);
		return processEngineConfiguration;
	}

	@Bean
	public ProcessEngine processEngine(ProcessEngineConfiguration processEngineConfiguration) {
		return processEngineConfiguration.buildProcessEngine();
	}

	@Bean
	public RepositoryService repositoryService(ProcessEngine processEngine) {
		return processEngine.getRepositoryService();
	}

	@Bean
	public RuntimeService runtimeService(ProcessEngine processEngine) {
		return processEngine.getRuntimeService();
	}

	@Bean
	public TaskService taskService(ProcessEngine processEngine) {
		return processEngine.getTaskService();
	}

	@Bean
	public HistoryService historyService(ProcessEngine processEngine) {
		return processEngine.getHistoryService();
	}

	@Bean
	public ManagementService managementService(ProcessEngine processEngine) {
		return processEngine.getManagementService();
	}

	@Bean
	public IdentityService identityService(ProcessEngine processEngine) {
		return processEngine.getIdentityService();
	}

	@Bean
	public FormService formService(ProcessEngine processEngine) {
		return processEngine.getFormService();
	}

}
